using System;
using System.Data;
using PCSComProduction.WorkOrder.DS;
using PCSComUtils.Common;
using System.Transactions;
using PCSComUtils.DataContext;
using PCSComUtils.DataAccess;
using System.Linq;
using System.Collections.Generic;

namespace PCSComProduction.WorkOrder.BO
{
    public class ApprovalWorkOrderCompletionBO
    {
        public DataSet SearchApprovalWOCompletion(int pintCcn, int intQaStatus, int pintWoMasterID, int intProLineId,
                                                  DateTime fromStartDate, DateTime toStartDate, string strFGoodsCode,
                                                  int intProductId)
        {
            ApprovalWorkOrderCompletionDS dsApprovalWOCompletion = new ApprovalWorkOrderCompletionDS();
            return dsApprovalWOCompletion.SearchApprovalWOCompletion(pintCcn, intQaStatus, pintWoMasterID, intProLineId,
                                                                     fromStartDate, toStartDate, strFGoodsCode,
                                                                     intProductId);
        }

        /// <summary>
        ///     Update the work order completion record based on QA Status
        /// </summary>
        /// <param name="listCompletions">List of work order completion to be updated</param>
        /// <returns></returns>
        public bool UnreleaseCompletion(List<PRO_WorkOrderCompletion> listCompletions)
        {
            using (var trans = new TransactionScope())
            {
                using (var dataContext = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        foreach (PRO_WorkOrderCompletion woCompletion in listCompletions)
                        {
                            #region bin cache

                            var objBin = dataContext.IV_BinCaches.FirstOrDefault(
                                e => e.LocationID == woCompletion.LocationID && e.BinID == woCompletion.BinID &&
                                     e.ProductID == woCompletion.ProductID);
                            if (objBin != null)
                            {
                                objBin.OHQuantity = objBin.OHQuantity ?? 0;
                                objBin.OHQuantity = objBin.OHQuantity - woCompletion.CompletedQuantity;
                            }

                            #endregion

                            #region location cache

                            var objLocation = dataContext.IV_LocationCaches.FirstOrDefault(
                                e => e.LocationID == woCompletion.LocationID &&
                                     e.ProductID == woCompletion.ProductID);
                            if (objLocation != null)
                            {
                                objLocation.OHQuantity = objLocation.OHQuantity ?? 0;
                                objLocation.OHQuantity = objLocation.OHQuantity - woCompletion.CompletedQuantity;
                            }

                            #endregion

                            #region master location cache

                            var objMasLocation = dataContext.IV_MasLocCaches.FirstOrDefault(
                                e => e.ProductID == woCompletion.ProductID &&
                                     e.MasterLocationID == woCompletion.MasterLocationID);
                            if (objMasLocation != null)
                            {
                                objMasLocation.OHQuantity = objMasLocation.OHQuantity ?? 0;
                                objMasLocation.OHQuantity = objMasLocation.OHQuantity - woCompletion.CompletedQuantity;
                            }

                            #endregion

                            // Update WorkOrderCompletion
                            var completion = dataContext.PRO_WorkOrderCompletions.FirstOrDefault(c => c.WorkOrderCompletionID == woCompletion.WorkOrderCompletionID);
                            if (completion != null)
                            {
                                completion.NGQuantity = woCompletion.NGQuantity;
                                completion.CompletedQuantity = woCompletion.CompletedQuantity;
                                completion.QAStatus = (int)QAStatusEnum.Failure;
                            }

                            dataContext.SubmitChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                trans.Complete();
            }

            return true;
        }

        /// <summary>
        ///     Release work order completion. Update quantity and the completion
        /// </summary>
        /// <param name="listCompletions"></param>
        /// <returns></returns>
        public bool ReleaseCompletion(List<PRO_WorkOrderCompletion> listCompletions)
        {
            using (var trans = new TransactionScope())
            {
                using (var dataContext = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        foreach (PRO_WorkOrderCompletion orderCompletion in listCompletions)
                        {
                            #region bin cache

                            var objBin = dataContext.IV_BinCaches.FirstOrDefault(
                                e => e.LocationID == orderCompletion.LocationID && e.BinID == orderCompletion.BinID &&
                                     e.ProductID == orderCompletion.ProductID);
                            if (objBin != null)
                            {
                                objBin.OHQuantity = objBin.OHQuantity + orderCompletion.CompletedQuantity;
                            }
                            else
                            {
                                var binCache = new IV_BinCache
                                                   {
                                                       CCNID = orderCompletion.CCNID,
                                                       MasterLocationID = orderCompletion.MasterLocationID,
                                                       LocationID = orderCompletion.LocationID
                                                   };
                                if (orderCompletion.BinID != null)
                                {
                                    int binId = orderCompletion.BinID ?? 0;
                                    binCache.BinID = binId;
                                }
                                binCache.ProductID = orderCompletion.ProductID;
                                binCache.OHQuantity = orderCompletion.CompletedQuantity;
                                dataContext.IV_BinCaches.InsertOnSubmit(binCache);
                            }

                            #endregion

                            #region location cache

                            var objLocation = dataContext.IV_LocationCaches.FirstOrDefault(
                                e => e.LocationID == orderCompletion.LocationID &&
                                     e.ProductID == orderCompletion.ProductID);
                            if (objLocation != null)
                            {
                                objLocation.OHQuantity = objLocation.OHQuantity ?? 0;
                                objLocation.OHQuantity = objLocation.OHQuantity + orderCompletion.CompletedQuantity;
                            }
                            else
                            {
                                // add new record
                                var locationCache = new IV_LocationCache
                                                        {
                                                            CCNID = orderCompletion.CCNID,
                                                            MasterLocationID = orderCompletion.MasterLocationID,
                                                            LocationID = orderCompletion.LocationID,
                                                            ProductID = orderCompletion.ProductID,
                                                            OHQuantity = orderCompletion.CompletedQuantity
                                                        };
                                dataContext.IV_LocationCaches.InsertOnSubmit(locationCache);
                            }

                            #endregion

                            #region master location cache

                            var objMasLocation = dataContext.IV_MasLocCaches.FirstOrDefault(
                                e => e.ProductID == orderCompletion.ProductID &&
                                     e.MasterLocationID == orderCompletion.MasterLocationID);
                            if (objMasLocation != null)
                            {
                                objMasLocation.OHQuantity = objMasLocation.OHQuantity ?? 0;
                                objMasLocation.OHQuantity = objMasLocation.OHQuantity +
                                                            orderCompletion.CompletedQuantity;
                                objMasLocation.SupplyQuantity = objMasLocation.SummItemCost21 = 0;
                            }
                            else
                            {
                                // add new record
                                var masLocCache = new IV_MasLocCache
                                                      {
                                                          CCNID = orderCompletion.CCNID,
                                                          MasterLocationID = orderCompletion.MasterLocationID,
                                                          ProductID = orderCompletion.ProductID,
                                                          OHQuantity = orderCompletion.CompletedQuantity
                                                      };
                                dataContext.IV_MasLocCaches.InsertOnSubmit(masLocCache);
                            }

                            #endregion
                            
                            // Update WorkOrderCompletion
                            var completion = dataContext.PRO_WorkOrderCompletions.FirstOrDefault(c => c.WorkOrderCompletionID == orderCompletion.WorkOrderCompletionID);
                            if (completion != null)
                            {
                                completion.NGQuantity = orderCompletion.NGQuantity;
                                completion.CompletedQuantity = orderCompletion.CompletedQuantity;
                                completion.QAStatus = (int)QAStatusEnum.Passed;
                            }

                            dataContext.SubmitChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }

                trans.Complete();
            }
            return true;
        }
    }
}
